package com.codechuan.hallo_web.controller;

import com.alibaba.fastjson.JSONObject;
import com.codechuan.hallo_web.common.Constant;
import com.codechuan.hallo_web.utils.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class TxmController {
    @GetMapping(value = "/initTxm")
    private String getIpAddress() {
        return Constant.IP_ADDRESS;
    }

    @GetMapping(value = "/getIp")
    private String getIp() {
        String url = "http://api2.xkdaili.com/tools/XApi.ashx?apikey=XK00CE13196F4A48C915&qty=1&format=json&split=0&sign=6ae82543fc3d5cad19ecc7c22efe21a7";

        String result = null;
        try {
            result = HttpUtil.doGet1(url);
        } catch (IOException e) {
            Constant.IP_ADDRESS = null;
        }
        Constant.NUMBER_BL++;

        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject != null) {
            String data = jsonObject.getString("data");
            String ip = JSONObject.parseObject(JSONObject.parseArray(data).getString(0)).getString("ip");
            String port = JSONObject.parseObject(JSONObject.parseArray(data).getString(0)).getString("port");
            return ip + ":" + port;
        } else {
            return "";
        }
    }
}
